package com.lantosec.hmrp.core.util.identify;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.net.URLEncoder;

//营业执照试别
public class BusinessLicense {
    public static BusinessLicenseBean businessLicense(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license";
        try {
            // 本地文件路径
            //String filePath = "";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, param);

            //封装对象
            BusinessLicenseBean bean = new BusinessLicenseBean();
            JSONObject jsonObject = JSON.parseObject(result);
            String companyName =  jsonObject.getJSONObject("words_result").getJSONObject("单位名称").getString("words");
            String type =  jsonObject.getJSONObject("words_result").getJSONObject("类型").getString("words");
            String legalPerson =  jsonObject.getJSONObject("words_result").getJSONObject("法人").getString("words");
            String address =  jsonObject.getJSONObject("words_result").getJSONObject("地址").getString("words");
            String validityDate =  jsonObject.getJSONObject("words_result").getJSONObject("有效期").getString("words");
            String idNumber =  jsonObject.getJSONObject("words_result").getJSONObject("证件编号").getString("words");
            String socialCreditCode =  jsonObject.getJSONObject("words_result").getJSONObject("社会信用代码").getString("words");
            bean.setCompanyName(companyName);
            bean.setType(type);
            bean.setLegalPerson(legalPerson);
            bean.setAddress(address);
            bean.setValidityDate(validityDate);
            bean.setIdNumber(idNumber);
            bean.setSocialCreditCode(socialCreditCode);

            System.out.println(result);
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*public static void main(String[] args) {
        BusinessLicense.businessLicense();
    }*/
}
